# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Quick Markup Language Translator

Pass a text string with QML (a Wiki-like language) in it and this function
will transform it into pure HTML
"""

def qml(text):
    def beginswith(text,key):
        return text[:len(key)] == key
    def processline(text):
        if beginswith(text,'!!!'):
            return '<H3>'+text[3:]+'</H3>'
        elif beginswith(text,'!!'):
            return '<H2>'+text[2:]+'</H2>'
        elif beginswith(text,'!'):
            return '<H1>'+text[1:]+'</H1>'
        if beginswith(text,'***'):
            return '<ul><li type="circle">'+text[3:]+'</li></ul>'
        elif beginswith(text,'**'):
            return '<ul><li type="square">'+text[2:]+'</li></ul>'
        elif beginswith(text,'*'):
            return '<ul><li>'+text[1:]+'</li></ul>'
        elif text == '----':
            return '<HR>'
        else:
            return text + '<br>'

    lines = []
    for line in text.splitlines(0):
        lines += [processline(line)+'\n']

    return ''.join(lines)

if __name__ == '__main__':
    x = """\
!This is a header
!!This is the text that follows the header.
!!!This is a level two header.
----
<TABLE>
      <TR><TD>This</TD><TD>is</td><td>a</td><td>Table</td></tr>
      <TR><TD>This</TD><TD>is</td><td>a</td><td>Table</td></tr>
      <TR><TD>This</TD><TD>is</td><td>a</td><td>Table</td></tr>
      <TR><TD>This</TD><TD>is</td><td>a</td><td>Table</td></tr>
</table>
Here is a list:
* item one
* item two
* item three
"""
    print qml(x)
